package sections;

import static objects.AdapletConstants.sNewLine;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.LinkedHashMap;
import java.util.Map;

import objects.IObject;
import objects.RelToRel;

public class RelToRelSection implements ISection
{
	/**
	 * 
	 */
	private static final long serialVersionUID = -7900998099069577870L;
	private Map<String, RelToRel> fRelToRelList;

	public RelToRelSection()
	{
		fRelToRelList = new LinkedHashMap<String, RelToRel>();
	}

	public RelToRel createNewRelToRel(String relationshipName)
	{
		RelToRel relationship = new RelToRel();

		relationship.setRelationshipName(relationshipName);

		fRelToRelList.put(relationshipName, relationship);

		return relationship;
	}

	public RelToRel getRelationship(String relName)
	{
		return fRelToRelList.get(relName);
	}

	@Override
	public String toString()
	{
		return buildRelToRelSectionDefinition();
	}

	public String buildRelToRelSectionDefinition()
	{
		StringBuffer relToRelSectionDef = new StringBuffer(500 * fRelToRelList.size());

		for (String relName : fRelToRelList.keySet())
		{
			RelToRel relToRel = fRelToRelList.get(relName);

			relToRelSectionDef.append(relToRel.toString());
			relToRelSectionDef.append(sNewLine);
		}

		return relToRelSectionDef.toString();
	}

	@Override
	public int getCount()
	{
		return fRelToRelList.size();
	}

	@Override
	public Section getSectionType()
	{
		return Section.REL_TO_REL;
	}

	@Override
	public IObject[] getObjects()
	{
		return fRelToRelList.values().toArray(new RelToRel[0]);
	}
	
	private void readObject(ObjectInputStream iStream) throws IOException, ClassNotFoundException
	{
		iStream.defaultReadObject();
		validateState();
	}
	
	private void validateState()
	{
		// TODO Auto-generated method stub
		
	}

	private void writeObject(ObjectOutputStream oStream) throws IOException
	{
		oStream.defaultWriteObject();
	}
}
